import os from "os"
import sqlite3 from 'sqlite3'
import fs from 'fs'
import DbUtil from './util'


const ENV = process.env.NODE_ENV

const homeDir =  os.homedir() + '\\'

let dbPath = ''
console.log('环境：' , ENV)
ENV === 'development' ?  dbPath = homeDir + '.devWechat' : dbPath = homeDir +  '.wechat'

// 初始化文件不存在则创建
if (!fs.existsSync(dbPath)) {
  fs.mkdirSync(dbPath)
}

const dbFile = dbPath + '\\weChat.db'

const db = new sqlite3.Database(dbFile , (err) => {
  if (err) {
    console.log('sqlLite数据库创建失败！' , err)
  } else {
    console.log('数据库实例创建成功！')
  }
})


const dbUtil = new DbUtil()
dbUtil.initDB(db).then(() => {
  console.log('数据库表初始化成功！')
})


export default db
